package com.thinkinghead.mahjongrecorder;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.parse.ParseUser;
import com.thinkinghead.mahjongrecorder.adaptor.GameListItemAdaptor;
import com.thinkinghead.mahjongrecorder.data.connect.ParseAdaptor;
import com.thinkinghead.mahjongrecorder.data.connect.ParseObjectConvertor;
import com.thinkinghead.mahjongrecorder.data.connect.RecordCallBack;
import com.thinkinghead.mahjongrecorder.model.Record;
import com.thinkinghead.mahjongrecorder.ui.dialog.DialogHelper;

public class GameListActivity extends Activity {
	private static final String TAG = "GameListActivity";
	private GameListItemAdaptor adp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game_list);
		fnClearTitle();
		fnInitView();
		fnLoadData();
	}

	private void fnInitView() {
		ListView listView = (ListView) findViewById(R.id.game_list);

		List<Record> list = new ArrayList<Record>();
		adp = new GameListItemAdaptor(this, list);
		listView.setAdapter(adp);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long id) {
				Log.d(TAG, "onItemClick" + position);
				Record r = adp.getItem(position);
				fnRecordSelected(r);
			}
		});

		listView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				Log.d(TAG, "onItemLongClick" + arg2);
				return false;
			}
		});
	}

	public void fnLoadData() {
		DialogHelper.fnShowDialog(this);
		RecordCallBack cb = new RecordCallBack() {
			@Override
			public void getRecordList(List<Record> recList) {
				for (int i = 0; i < recList.size(); i++) {
					adp.addItems(recList.get(i));
				}
				adp.update();
				DialogHelper.fnCloseDialog();
			}
		};
		ParseAdaptor pa = new ParseAdaptor(this);
		pa.getRecordList(cb);
	}

	private void fnClearTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.action_menu, menu);
		return true;
	}

	/*
	 * Creating posts and refreshing the list will be controlled from the Action
	 * Bar.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_refresh: {
			fnRefresh();
			break;
		}

		case R.id.action_new: {
			fnNewGame();
			break;
		}

		case R.id.action_add: {
			fnAddGame();
			break;
		}

		case R.id.action_person: {
			fnEditUsr();
			break;
		}
		case R.id.action_logout: {
			fnLogout();
			break;
		}

		}
		return super.onOptionsItemSelected(item);
	}

	public void fnNewGame() {
		Intent myIntent = new Intent(this, NewRecordActivity.class);
		this.startActivity(myIntent);
	}

	public void fnAddGame() {
		Intent intent = new Intent(this, AddReferenceRecordActivity.class);
		this.startActivity(intent);
	}

	private void fnEditUsr() {
		Intent intent = new Intent(this, EditUserActivity.class);
		this.startActivity(intent);
	}

	private void fnLogout() {
		ParseUser.logOut();
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
		this.finish();
	}

	private void fnRefresh() {
		adp.removeAll();
		fnLoadData();
		/*
		 * Log.d(TAG, "fnRefresh, check usr");
		 * ParseUser.getCurrentUser().saveInBackground(new SaveCallback() {
		 * 
		 * @Override public void done(ParseException e) { if (e == null) {
		 * DialogHelper.fnCloseDialog(); ParseUser cUsr =
		 * ParseUser.getCurrentUser(); Log.d(TAG, "Usr ID  : " +
		 * cUsr.getObjectId()); Log.d(TAG, "Usr Name  : " + cUsr.getUsername());
		 * Log.d(TAG, "Usr authData  : " + cUsr.get("authData")); } else {
		 * Toast.makeText(getApplicationContext(), "Error saving: " +
		 * e.getMessage(), Toast.LENGTH_SHORT).show(); } } });
		 */
	}

	private void fnRecordSelected(Record li) {
		Log.d(TAG, "onSelected" + li);
		if (li != null) {
			Intent mIntent = new Intent(this, GameDetailActivity.class);
			Bundle args = new Bundle();
			args.putParcelable(ParseObjectConvertor.ARG_ITEM_ID, li);
			mIntent.putExtras(args);
			startActivity(mIntent);
		}
	}
}
